// login.js
import LOGIN from '../../API/LOGIN';
var app = getApp()
import APIURL from '../../const/APIURL'
import toolTip from '../tooltip/toolTip.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {  
  imgurl: '',
  userName:'',
  passWord:'',
  vcode:'',
  fangbiannum:0  
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    toolTip.init(this);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
    checkguid();
    this.setData({
      guid: app.globalData.guid,
      imgurl: APIURL.GETCAPCHA + "?guid=" + app.globalData.guid + "&time=" + new Date().getTime()
    })

  },
  refreshimg:function(){
    checkguid();
    var that=this;
    this.setData({     
      imgurl: APIURL.GETCAPCHA + "?guid=" + app.globalData.guid + "&time=" + new Date().getTime()
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },  
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  userNameInput:function(e)
  {
    this.setData({
      userName: e.detail.value
    })
  }
  ,
  passWordInput:function(e)
  {
    this.setData({
      passWord: e.detail.value
    })
  },
  vcodeInput: function (e) {
    this.setData({
      vcode: e.detail.value
    })
  }
  ,
  resetall:function()
  {
    this.setData({
      passWord: '',
      userName:'',
      vcode:''
    })
  },
  guestkLogin: function () {
    var that = this;    
    wx.showLoading({
      title: '加载中',
    })
    LOGIN.GuestLogin().then(function (d) {
      if (d.responseResult != 1) {
        wx.showToast({
          title: d.msg || '出现错误',
          icon: 'loading',
          duration: 1000,
          mask: true
        })

        toolTip.showToolTip('error', d.msg, 3000);
        that.refreshimg();
      }
      else {
        app.globalData.role='guest'
        app.globalData.token = d.data;
        wx.setStorageSync('token', d.data)
        wx.hideLoading()
        wx.switchTab(
          {
            url: "/pages/department/department"
          }
        )
      }
      console.log(d);

    }, function () {
      wx.hideLoading()
    });
  },
  fangbian:function()
  {
    var that=this;
    if (that.data.fangbiannum<10)
    {
      that.setData({ fangbiannum: that.data.fangbiannum+1})
      return;
    }
    wx.showLoading({
      title: '加载中',
    })
   LOGIN.FangBian().then(function (d) {
      if (d.responseResult != 1) {
        wx.showToast({
          title: d.msg||'出现错误',
          icon: 'loading',
          duration: 1000,
          mask: true
        })
       
        toolTip.showToolTip('error', d.msg, 3000);
       
        that.refreshimg();
      }
      else {
        app.globalData.token = d.data;
        app.globalData.role = 'admin'
        wx.setStorageSync('token', d.data)
        wx.hideLoading()
        wx.switchTab(
          {
            url: "/pages/sutlist/stulist"
          }
        )
      }
      console.log(d);
     
    }, function () {
      wx.hideLoading()
    });
  }
  ,
  checkLogin:function()
  { 
    
      this.setData({ showTopTips: false });
      var that=this;    
      var data=this.data;
      if (!data.userName || !data.passWord || !data.vcode)
      {
        wx.showToast({
          title: '表单不完整，请填写完整！',
          image:'/images/logo.png'
        })
        toolTip.showToolTip('error', '表单不完整，请填写完整！', 2000);
        return;
      }
      var formdata={
        username: data.userName,
        password:data.passWord,
        vcode:data.vcode,
        guid: app.globalData.guid,
      }
      wx.showLoading({
        title: '加载中',
      })

      
      LOGIN.CheckLogin(formdata).then(function(d){
         if (d.responseResult != 1)
         {
           wx.showToast({
             title: d.msg||'出现错误！',
             icon: 'loading',
             duration: 1000,
             mask: true
           })
           toolTip.showToolTip('error', d.msg, 2000);        
               that.refreshimg();
         }
         else
         {
           app.globalData.role = 'admin'
           app.globalData.token=d.data;
           wx.setStorageSync('token', d.data)
           wx.switchTab(
             {
               url: "/pages/sutlist/stulist"
             }
           )
         }
        console.log(d);
        wx.hideLoading()
      },function(){
         wx.hideLoading()
      });
  }
})
function checkguid(){ 
  LOGIN.CheckGuid(app.globalData.guid).then(function(d){    
    if (d.responseResult!=1)
    {
      console.log("GUID错误！");
      LOGIN.GetGuid().then(function (d) {
        console.log(d)       
        app.globalData.guid = d.data
        wx.setStorageSync('guid', d.data)
      })
    }
    else{
      console.log("GUID正常！");       
    }
   
  })
}